package com.aischool.api;

import com.aischool.common.model.AiCourse;
import com.aischool.common.model.AiCourseClass;
import com.aischool.params.AiCourseParams;
import com.aischool.routes.RouteMapping;
import com.aischool.service.AiArticlecatService;
import com.aischool.service.AiCourseClassService;
import com.aischool.service.AiCourseService;
import com.aischool.utils.AppUtil;
import com.aischool.utils.DateUtil;
import com.aischool.utils.JsonCodeEnum;
import com.aischool.utils.JsonResult;
import com.aischool.utils.RegexUtil;
import com.aischool.utils.StateEnum;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;

@RouteMapping(url = "/api/course")
public class AiCourseController extends Controller {
	
	@Inject
	AiCourseService aiCourseService;
	@Inject
	AiCourseClassService aiCourseClassService;
	@Inject
	AiArticlecatService aiArticlecatService;

	public void getPage() {
		// 结果类
		JsonResult jr = new JsonResult();
		// 参数类
		AiCourseParams params = new AiCourseParams();
		// 获取参数
		// int initno = getInt("initno", 0);
		int poid = getParaToInt("poid",0);
		
		params.setPageNo(getParaToInt("pageNo", 1));
		params.setPageSize(getParaToInt("pageSize", AppUtil.Default_PageSize));
//		if (getParaToInt("state")!=null) {
//			params.setState(getParaToInt("state"));
//		}
		params.setState(getInt("state", StateEnum.ALL.getState()));
		if (StrKit.notBlank(get("keywords",""))) {
			params.setKeywords(get("keywords",""));
		}
		params.setPoid(poid);

		// 获取分页
		Object pageData = aiCourseService.getPage(params);
		
		jr.setDicKV("pageData", pageData);
		
		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
	}
	
	/**
	 * 获取单个用户
	 */
	public void getOne() {
		// 结果类
		JsonResult jr = new JsonResult();
		// 获取单个用户信息
		int course_id = getParaToInt("id",0);
		// 详情页
		int idesc = getParaToInt("idesc",0);
		
		Object objcat = aiArticlecatService.getAll(0, 1);
		if (objcat != null) { jr.setDicKV("listcat", objcat); }
		
		if (course_id > 0) {
			Object obj = aiCourseService.getOne(course_id);
			if (obj != null) { jr.setDicKV("oneinfo", obj); }
		}
		if (idesc > 0) {
			Object obj = aiCourseClassService.getClass(course_id);
			if (obj != null) { jr.setDicKV("listclass", obj); }
			
		}

		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
	}
	
	public void save() {
		// 结果类
		JsonResult jr = new JsonResult();
		// 表单转换类
		int course_id = getParaToInt("course_id", 0);
		AiCourse info = getModel(AiCourse.class, "", true);

		if (StrKit.isBlank(info.getCourseName())) {
			jr.appendMsg("请输入课程名称！");
		}else if (StrKit.isBlank(info.getCourseDesc())) {
			jr.appendMsg("请输入课程简介！");
		}else {
			
			info.setUpdateTime(DateUtil.getTimestamp());
			
			info.removeNullValueAttrs();
			
			boolean bf = false;
			if (course_id >0) {
				// 更新
				bf = info.update();
			}else {
				// 插入
				info.setAddTime(DateUtil.getTimestamp());
				bf = info.save();
			}
			
			if (bf) {
				jr.setEnum(JsonCodeEnum.Success);
			}else {
				jr.setEnum(JsonCodeEnum.Faild);
			}
		}
		renderJson(jr.Dic);
	}
	
	public void del() {
		// 结果类
		JsonResult jr = new JsonResult();
		boolean bf = false;
		// 表单转换类
		int course_id = getParaToInt("id", 0);
		if (course_id > 0) {
			bf = aiCourseService.del(course_id);
		}else {
			String ids = get("ids","");
			bf = aiCourseService.delMore(ids);
		}
		
		if (bf) {
			jr.setEnum(JsonCodeEnum.Success);
		}else {
			jr.setEnum(JsonCodeEnum.Faild);
		}
		
		renderJson(jr.Dic);
	}
	
	// 课程详情
	public void saveClass() {
		// 结果类
		JsonResult jr = new JsonResult();
		// 表单转换类
		int class_id = getInt("class_id", 0);
		int parent_id = getInt("parent_id",0);
		AiCourseClass info = getModel(AiCourseClass.class, "", true);
		
		if (StrKit.isBlank(info.getClassTitle())) {
			jr.appendMsg("请输入名称！");
		}else if (info.getClassSort() == null || info.getClassSort() <= 0) {
			jr.appendMsg("请输入排序！");
		}else if (parent_id == 1 && StrKit.isBlank(getPara("file_url"))) {
			jr.appendMsg("请先上传文件！");
		}else if (parent_id == 1 && StrKit.notBlank(getPara("time_len")) && !RegexUtil.isInteger(getPara("time_len"))) {
			jr.appendMsg("音视频时长应为数字！");
		}else {
			
			info.setUpdateTime(DateUtil.getTimestamp());
			info.setClassState(1);
			
			info.removeNullValueAttrs();
			
			boolean bf = false;
			if (class_id >0) {
				// 更新
				bf = info.update();
			}else {
				// 插入
				info.setAddTime(DateUtil.getTimestamp());
				bf = info.save();
			}
			
			if (bf) {
				jr.setEnum(JsonCodeEnum.Success);
			}else {
				jr.setEnum(JsonCodeEnum.Faild);
			}
		}
		renderJson(jr.Dic);
		
	}
	
	public void delClass() {// 结果类
		JsonResult jr = new JsonResult();
		boolean bf = false;
		// 表单转换类
		int class_id = getParaToInt("class_id", 0);
		if (class_id > 0) {
			int ir = aiCourseClassService.delClass(class_id, PathKit.getWebRootPath());
			bf = ir > 0;
		}
		
		if (bf) {
			jr.setEnum(JsonCodeEnum.Success);
		}else {
			jr.setEnum(JsonCodeEnum.Faild);
		}
		
		renderJson(jr.Dic);
	}
}
